package org.automated.realestatemanager.parsers;

import java.util.Set;

import org.apache.log4j.Logger;
import org.automated.realestatemanager.dataanaliser.Analyzer;
import org.automated.realestatemanager.db.entity.Advertisement;
import org.automated.realestatemanager.db.entity.Agency;
import org.automated.realestatemanager.db.entity.Phone;
import org.automated.realestatemanager.db.entity.Publisher;
import org.automated.realestatemanager.db.services.ServiceFactory;
import org.automated.realestatemanager.parserutils.ParserUtils;

public class AgencyParser implements AdvertisementParser {

	private static final Logger log = Logger.getLogger(AgencyParser.class);

	Agency agency = null;
	int counter = 0;
	Advertisement advertisement = null;
	Publisher publisher = null;
	String source = "";
	String name = "";
	String status = "";

	public AgencyParser(String agency) {
		this.agency = ServiceFactory.getInstance().getAgencyService()
				.getAgencyByName(agency);
		if (this.agency == null) {
			ServiceFactory.getInstance().getAgencyService()
					.create(new Agency(agency));
			this.agency = ServiceFactory.getInstance().getAgencyService()
					.getAgencyByName(agency);
		}
	}

	public void executeParcing(String text) {
		if (text.replaceAll("\\s", "").isEmpty()) {
			log.info("Empty string");
			return;
		}
		counter++;
		log.info("Parsing advertiusement #" + counter + " " + text);

		Set<Phone> phones = ParserUtils.parsePhones(text);

		publisher = Analyzer.getPhonesOwner(phones);

		phones = ServiceFactory.getInstance().getPhoneService()
				.persistPhones(phones);

		if (phones.size() == 0)
			return;

		boolean publisherExist = false;
		if (publisher == null) {
			log.debug("Publisher not found");
			publisher = new Publisher();
			if (this.name.isEmpty()) {
				publisher.setName(ParserUtils.parseName(text));
			} else {
				publisher.setName(this.name);
			}
		} else {
			log.debug("Publisher found");
			publisherExist = true;
		}

		if (publisher.getAgency() == null) {
			publisher.setAgency(agency);
		}
		if (publisher.getName() == null || publisher.getName().isEmpty()) {
			publisher.setName(this.name);
		}
		if ((publisher.getStatus() == null || publisher.getStatus().isEmpty())) {
			publisher.setStatus(this.status);
		}
		if ((publisher.getSource() == null || publisher.getSource().isEmpty())) {
			publisher.setSource(this.source);
		}

		publisher.getPhones().addAll(phones);
		publisher.getAdvertisements().add(advertisement);
		if (publisherExist) {
			ServiceFactory.getInstance().getPublisherService().merge(publisher);
		} else {
			ServiceFactory.getInstance().getPublisherService()
					.create(publisher);
		}
		log.debug("Publisher saved");
		phones.clear();
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}